package com.htt.app.babau.sqlite;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.htt.app.babau.objects.Categories;
import com.htt.app.babau.utils.Log;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DAOCategories {
	 
    protected static final String TAG = "DAOCategories"; 
    
	private String[] allColumns = { MyDataBaseHelper.CategoriesId,
		      MyDataBaseHelper.CategoriesName,	
		      MyDataBaseHelper.CategoriesImage};
	
    private Context context; 
    private SQLiteDatabase db; 
    private MyDataBaseHelper dbHelper; 
 
    public DAOCategories(Context context)  
    { 
        this.context = context; 
        dbHelper = new MyDataBaseHelper(context); 
    } 
 
    public DAOCategories createDatabase() throws SQLException  
    { 
    	synchronized (MyDataBaseHelper.Lock) {
    		try  
            { 
                dbHelper.createDataBase(); 
            }  
            catch (IOException mIOException)  
            { 
                Log.e(TAG, mIOException.toString() + "  UnableToCreateDatabase"); 
            } 
            return this; 
		}
        
    } 
 
    public DAOCategories open() throws SQLException
    { 
    	synchronized (MyDataBaseHelper.Lock) {
    		try  
            { 
                dbHelper.openDataBase(); 
                dbHelper.close(); 
                db = dbHelper.getReadableDatabase(); 
            }  
            catch (SQLException mSQLException)  
            { 
                Log.e(TAG, "open >>"+ mSQLException.toString()); 
            } 
            return this; 
		}
        
    } 
 
    public void close()  
    { 
    	if(db != null)
        db.close(); 
    } 
 
 // Adding
    public void insertCategories(Categories item) {
    	try {
		
	        ContentValues values = new ContentValues();
	        values.put(MyDataBaseHelper.CategoriesId, item.getId()); 
	        values.put(MyDataBaseHelper.CategoriesName, item.getName());
	        values.put(MyDataBaseHelper.CategoriesImage, item.getImage());
	        
	        // Inserting Row
	        if(db.insert(MyDataBaseHelper.Categories, null, values)!=-1)
	        	Log.w(TAG, "insert sucess");
    	
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
    }
    
    // delete
    public void deleteCategories(Categories item) {
    	try {
    		db.delete(MyDataBaseHelper.Categories, MyDataBaseHelper.CategoriesId + " = " + item.getId(), null);
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    }
    
    public void deleteAllCategories() {
    	try {
	    	int doneDelete = 0;
	        doneDelete = db.delete(MyDataBaseHelper.Categories, null, null);
	        Log.w(TAG, Integer.toString(doneDelete));
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    }
    
    // update
    public void updateCategories(Categories item) {
    	try {
	    	ContentValues values = new ContentValues();
	    	values.put(MyDataBaseHelper.CategoriesId, item.getId()); 
	        values.put(MyDataBaseHelper.CategoriesName, item.getName());
	        values.put(MyDataBaseHelper.CategoriesImage, item.getImage());
	    
	        db.update(MyDataBaseHelper.Categories, values, MyDataBaseHelper.CategoriesId + " = " + item.getId(), null);
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    }
    
    // getting single
    public Categories getCategories(int id) {
    	try {
	    	Cursor cursor = db.query(MyDataBaseHelper.Categories, allColumns, MyDataBaseHelper.CategoriesId + " = " + id, 
	    			null, null, null, null);
	    	if(cursor != null && cursor.getCount() > 0) {
	    		cursor.moveToFirst();
	    		Categories item = cursorToCategories(cursor);
		    	if(cursor != null && !cursor.isClosed())
		    		cursor.close();
		    	return item; 
	    	} else
	    		return null;
    	} catch (Exception e) {
    		e.printStackTrace();
    		return null;
    	}
    }
    
    // getting all
    public List<Categories> getAllCategories() {
    	try {
    	List<Categories> list = new ArrayList<Categories>();
    	
    	String selectQuery = "SELECT  * FROM " + MyDataBaseHelper.Categories;
    	Cursor cursor = db.rawQuery(selectQuery, null);
    	if(cursor != null && cursor.getCount() > 0) {
	    	if(cursor.moveToFirst()) {
	    		do{
	    			Categories item = cursorToCategories(cursor);
	    			list.add(item);
	    		} while(cursor.moveToNext());
	    	}
	    	
	    	if(cursor != null && !cursor.isClosed())
	    		cursor.close();
	    	
	    	return list;
    	} else
    		return null;
    	} catch (Exception e) {
			// TODO: handle exception
    		e.printStackTrace();
    		return null;
		}
    }
    
	 // Getting  Count
	 public int getCategoriesCount() {
		try {
	        String countQuery = "SELECT  * FROM " + MyDataBaseHelper.Categories;
	        Cursor cursor = db.rawQuery(countQuery, null);
	        cursor.close();
	 
	        // return count
	        return cursor.getCount();
		} catch(Exception e) {
			e.printStackTrace();
			return 0;
		}
    }
    
    // Channel from cursor
    private Categories cursorToCategories(Cursor cursor) {
    	Categories item = new Categories();
    	try {
	    	item.setId(cursor.getInt(0));
	    	item.setName(cursor.getString(1));
	    	item.setImage(cursor.getString(2));
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
        return item;
      }
}
